﻿package Interface 
{
	import caurina.transitions.Tweener;
	
	import Core.Page;
	import Core.PlayerInfo;
	
	import Interface.BattleStartClasses.PlayerInfoBlock;
	
	import GameUtils.GameDataEvent;
	
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.Event;	
	import flash.display.MovieClip;
		
	public class PageBattleStart extends Page 
	{
		public const COUNT_ENDED:String = 'COUNT_ENDED';
		
		private var FCounterText:TextField;
		private var FVersusText:TextField;
		private var FLoadingText:TextField;
		private var FCounterHeaderText:TextField;		
		private var FControlsText:MovieClip;	
		private var FTimer:Timer;
		private var FAvatarsTimer:Timer;
		private var FUser:PlayerInfo;
		private var FEnemy:PlayerInfo;
		private var FUserInfo:PlayerInfoBlock;
		private var FEnemyInfo:PlayerInfoBlock;
		
		public function PageBattleStart():void
		{
			FCounterText = getChildByName('CounterText') as TextField;
			FVersusText = getChildByName('VersusText') as TextField;
			FLoadingText = getChildByName('LoadingText') as TextField;			
			FCounterHeaderText = getChildByName('CounterHeaderText') as TextField;			
			FControlsText = getChildByName('ControlsText') as MovieClip;	
			FUserInfo = getChildByName('PlayerBlock') as PlayerInfoBlock;
			FEnemyInfo = getChildByName('EnemyBlock') as PlayerInfoBlock;
			
			Reset();
			
			FTimer = new Timer(1000);
			FTimer.addEventListener(TimerEvent.TIMER, OnTimer);
			FAvatarsTimer = new Timer(1000);
			FAvatarsTimer.addEventListener(TimerEvent.TIMER, OnAvatarsTimer);						
		}
		
		private function Reset():void
		{
			FControlsText.alpha = 0;
			FEnemy = null;
			FUserInfo.x = -300;
			FEnemyInfo.x = 700;
			FCounterText.visible = false;
			FVersusText.visible = false;
			FLoadingText.visible = false;
			FCounterHeaderText.visible = false;
		}
		
		private function OnTimer(e:TimerEvent):void
		{			
			if(FTimer.currentCount <= 2)
				FCounterText.text = (3 - FTimer.currentCount).toString() + '...';
			else
			{
				var vEvent:GameDataEvent = new GameDataEvent(COUNT_ENDED);
				vEvent.Data = FEnemy;
				dispatchEvent(vEvent);
				
				FTimer.stop();
				FTimer.reset();
				Reset();
			}
		}
		
		private function OnAvatarsTimer(e:TimerEvent):void
		{	
			if(FAvatarsTimer.currentCount >= 3)
			{							
				Tweener.addTween(FUserInfo, {x:-300, time:1, transition: "easeOutQuint"});
				Tweener.addTween(FEnemyInfo, {x:700, time:1, transition: "easeOutQuint"});
				Tweener.addTween(FVersusText, {alpha:0, time:1, transition: "easeOutQuint", onComplete:OnAvatarsHided});
			}
			
			function OnAvatarsHided():void
			{
				FVersusText.visible = false;	
				StartCount();
				FAvatarsTimer.stop();
				FAvatarsTimer.reset();
			}
		}
		
		public function HideInterface():void
		{						
			FGame.Interface.addEventListener(FGame.Interface.INTERFACE_HIDED, OnInterfaceHide);
			FGame.Interface.Hide();			
		}
		
		private function OnInterfaceHide(e:Event):void
		{
			FGame.CurrentPage = FGame.BattlesStart;
			Tweener.addTween(FControlsText, {alpha:1, time:1, transition: "easeOutQuint"});
			
			if(FEnemy)			
				ShowAvatars();
			else			
				StartCount();			
				
			FGame.Interface.removeEventListener(FGame.Interface.INTERFACE_HIDED, OnInterfaceHide);
		}
		
		private function StartCount():void
		{
			FCounterHeaderText.visible = true;
			FCounterText.visible = true;
			FCounterText.text = '3...';
			FGame.CurrentPage = FGame.BattlesStart;			
			FCounterText.alpha = 0;			
			Tweener.addTween(FCounterText, {alpha:1, time:1, transition: "easeOutQuint"});			
			FTimer.start();
			FGame.Interface.removeEventListener(FGame.Interface.INTERFACE_HIDED, OnInterfaceHide);
		}
		
		private function ShowAvatars():void
		{
			FUserInfo.User = FGame.User;			
			FEnemyInfo.User = FEnemy;
			FVersusText.visible = true;
			FVersusText.alpha = 0;
			Tweener.addTween(FUserInfo, {x:20, time:1, transition: "easeOutQuint"});
			Tweener.addTween(FEnemyInfo, {x:380, time:1, transition: "easeOutQuint"});
			Tweener.addTween(FVersusText, {alpha:1, time:1, transition: "easeOutQuint"});
			FAvatarsTimer.start();
		}
		
		public function set Enemy(AEnemy:String):void
		{
			if(AEnemy!='')
			{
				FCounterText.visible = true;
				FCounterText.text = 'Загрузка информации...';
				FGame.UsersLoader.addEventListener(FGame.UsersLoader.USER_LOADED, OnEnemyInfoLoaded);
				FGame.UsersLoader.Load(AEnemy);
			}
			else
			{
				HideInterface();
			}
		}		
		
		private function OnEnemyInfoLoaded(e:GameDataEvent):void
		{
			FCounterText.text = '';
			FEnemy = PlayerInfo(e.Data);
			HideInterface();
			FGame.UsersLoader.removeEventListener(FGame.UsersLoader.USER_LOADED, OnEnemyInfoLoaded);
		}
	}	
}
